package com.ruoyi.project.cwsk.channelsub.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cwsk.channelsub.domain.ChannelSubUser;
import com.ruoyi.project.cwsk.channelsub.service.IChannelSubUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 渠道管理Controller
 * 
 * @author songzc
 * @date 2020-01-03
 */
@Controller
@RequestMapping("/cwsk/channelsub")
public class ChannelSubUserController extends BaseController
{
    private String prefix = "cwsk/channelsub";

    @Autowired
    private IChannelSubUserService channelSubUserService;

    @RequiresPermissions("cwsk:channelsub:view")
    @GetMapping()
    public String channelsub()
    {
        return prefix + "/channelsub";
    }

    /**
     * 查询渠道管理列表
     */
    @RequiresPermissions("cwsk:channelsub:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChannelSubUser channelSubUser)
    {
        start2Page();
        List<ChannelSubUser> list = channelSubUserService.selectChannelSubUserList(channelSubUser);
        return getDataTable(list);
    }

    /**
     * 导出渠道管理列表
     */
    @RequiresPermissions("cwsk:channelsub:export")
    @Log(title = "渠道管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelSubUser channelSubUser)
    {
        List<ChannelSubUser> list = channelSubUserService.selectChannelSubUserList(channelSubUser);
        ExcelUtil<ChannelSubUser> util = new ExcelUtil<ChannelSubUser>(ChannelSubUser.class);
        return util.exportExcel(list, "channelsub");
    }

    /**
     * 新增渠道管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存渠道管理
     */
    @RequiresPermissions("cwsk:channelsub:add")
    @Log(title = "渠道管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChannelSubUser channelSubUser)
    {
        try {
        	channelSubUser.setChannelName(StringUtils.trim(StringUtils.replaceBlank(channelSubUser.getChannelName())));
        	channelSubUser.setChannelAcronym(StringUtils.replaceBlank(channelSubUser.getChannelAcronym()));
        	channelSubUser.setContact(StringUtils.replaceBlank(channelSubUser.getContact()));
			
			int rows=channelSubUserService.insertChannelSubUser(channelSubUser);
			return toAjax(rows);
		} catch (org.springframework.dao.DuplicateKeyException e) {
			return error("渠道添加失败:账号重复!");
		} catch (Exception e) {
			e.printStackTrace();
			return error("渠道添加失败,请联系管理员!");
		}
    }

    /**
     * 修改渠道管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ChannelSubUser channelSubUser = channelSubUserService.selectChannelSubUserById(id);
        mmap.put("channelSubUser", channelSubUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存渠道管理
     */
    @RequiresPermissions("cwsk:channelsub:edit")
    @Log(title = "渠道管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChannelSubUser channelSubUser)
    {
        return toAjax(channelSubUserService.updateChannelSubUser(channelSubUser));
    }

    /**
     * 删除渠道管理
     */
    @RequiresPermissions("cwsk:channelsub:remove")
    @Log(title = "渠道管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(channelSubUserService.deleteChannelSubUserByIds(ids));
    }
}
